package com.example.demo1.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo1.common.Result;
import com.example.demo1.entity.Project;
import com.example.demo1.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@ResponseBody
@RequestMapping("project")
public class ProjectController {
    @Autowired
    ProjectService projectService;

    @PostMapping("/add")
    public Result addProject(@RequestBody Project project){
        try{
            projectService.addProject(project);
            return Result.success("success");
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }

    @PostMapping("/update")
    public Result updateProject(@RequestBody Project project){
        try{
            projectService.updateProject(project);
            return Result.success("success");
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }

    @PostMapping("/delete")
    public Result deleteProject(@RequestBody Project project){
        try {
            projectService.deleteProjrct(project);
            return Result.success("success");
        }catch (Exception e){
            return Result.fail(e.toString());
        }
    }


//    @PathVariable获取路径上的参数，@RequestBody获取请求体
    @PostMapping("/list/{currentPage}/{pageSize}")
    public Result getProject(@PathVariable int currentPage,
                             @PathVariable int pageSize,
                             @RequestBody Project project){
        IPage<Project> IPageProject=projectService.getProjectList(currentPage, pageSize, project);
        return Result.success(IPageProject);
    }
    @GetMapping("/list/all")
    public Result getProjectAll() {
        return Result.success(projectService.getProjectAll());
    }

}
